package com.oren.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author wangshiwei
 * @create 2018/7/7 16:59
 * @desc 代理管理器
 **/
public class ProxyManager {

    /**
     * 调用cglib里面的代理方法 来执行代理
     * @param targerClass 目标代理类
     * @param proxyList 所有代理的方法
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T createProxy(final Class<?> targerClass,final List<Proxy> proxyList){
        return (T) Enhancer.create(targerClass, new MethodInterceptor() {
            //@Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
               return new ProxyChain(targerClass,o,method,methodProxy,objects,proxyList).doProxyChain();
            }
        });
    }


}
